using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DTO;
using BUS;
using Utility;

public partial class admin_TraLoi : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadData();
    }

    private void LoadData()
    {
        Pager.PageSize = 5;
        Criteria c = new Criteria();
        if (txt_ID.Text != "")
        {
            c.add(Condition.AND, TraLoiColumns.ID, Condition.LIKE, txt_ID.Text);
        }
        if (ddl_KhachHang.SelectedValue != "")
        {
            c.add(Condition.AND, TraLoiColumns.MaKH, Condition.EQUAL,
               int.Parse(ddl_KhachHang.SelectedValue));
        }
        int total = 0;
        TraLoiCollection traLoiCollection =
            TraLoi_BUS.Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
            TraLoiColumns.ID.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = traLoiCollection;
        Repeater1.DataBind();

        Pager.ItemCount = total;
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
        e.Item.ItemType == ListItemType.Item)
        {
            TraLoi_DTO traLoi = (TraLoi_DTO)e.Item.DataItem;
            HyperLink hyp_ID = (HyperLink)e.Item.FindControl("ID");

            Literal khach_hang = (Literal)e.Item.FindControl("l_khachHang");
            KhachHang_DTO khachHang = KHACHHANG_BUS.GetByID(traLoi.MaKH);
            khach_hang.Text = khachHang.HoTen;

            hyp_ID.Text = traLoi.ID.ToString();
            hyp_ID.NavigateUrl = "~/Admin/A_ChiTiet_TraLoi.aspx?id=" + traLoi.ID.ToString();

        }
    }
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int id = int.Parse(hf_id.Value);
                TraLoi_DTO traLoi = TraLoi_BUS.GetByID(id);

                TraLoi_BUS.Delete(id);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        LoadData();
    }
}
